package org.findbugs.report.entity;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Node;
import org.findbugs.report.util.XPathExpressions;

public class ClassStats {

    private String clazz;
    private int size;
    private int bugs;

    /**
     * Initialize class stats for a given package
     * @param findbugsDocument
     * @param pack
     * @return
     */
    public static List<ClassStats> getClassStats(Document findbugsDocument, String pack) {
        List<ClassStats> result = new LinkedList<ClassStats>();
        List<Node> classStatsNodes = findbugsDocument.selectNodes(XPathExpressions.PACKAGE_STATS + 
                "[package='" + pack + "']/ClassStats");
        Iterator<Node> it = classStatsNodes.iterator();
        while (it.hasNext()) {
            Node classStatsNode = it.next();
            ClassStats classStats = new ClassStats();
            classStats.setSize(Integer.parseInt(classStatsNode.selectSingleNode("@size").getText()));
            classStats.setBugs(Integer.parseInt(classStatsNode.selectSingleNode("@bugs").getText()));
            classStats.setClazz(classStatsNode.selectSingleNode("@class").getText());
            result.add(classStats);
        }
        return result;
    }

    public String getClazz() {
        return clazz;
    }

    public int getSize() {
        return size;
    }

    public int getBugs() {
        return bugs;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void setBugs(int bugs) {
        this.bugs = bugs;
    }
}
